﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.RoboticOperationMerge.CApROMDataManager
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.RoboticOperationMerge;

internal class CApROMDataManager
{
  private List<CUiROMTargetInfo> m_targetChildren;
  private List<CUiROMSourceInfo> m_sourceChildren;
  private CUiROMRoboticTargetOperationInfo m_targetOperationAtTop;
  private CUiROMRoboticSourceOperationInfo m_sourceOperationAtTop;
  private TxObjectList m_sharedWeldPoints;
  private TxOLPUtilities m_olpUtils;

  public CApROMDataManager()
  {
    this.m_targetChildren = new List<CUiROMTargetInfo>();
    this.m_sourceChildren = new List<CUiROMSourceInfo>();
    this.m_targetOperationAtTop = (CUiROMRoboticTargetOperationInfo) null;
    this.m_sourceOperationAtTop = (CUiROMRoboticSourceOperationInfo) null;
    this.m_sharedWeldPoints = new TxObjectList();
    this.m_olpUtils = new TxOLPUtilities();
  }

  public List<CUiROMTargetInfo> TargetChildren
  {
    get => this.m_targetChildren;
    set => this.m_targetChildren = value;
  }

  public List<CUiROMSourceInfo> SourceChildren
  {
    get => this.m_sourceChildren;
    set => this.m_sourceChildren = value;
  }

  public CUiROMRoboticTargetOperationInfo TargetOperation
  {
    get => this.m_targetOperationAtTop;
    set => this.m_targetOperationAtTop = value;
  }

  public CUiROMRoboticSourceOperationInfo SourceOperation
  {
    get => this.m_sourceOperationAtTop;
    set => this.m_sourceOperationAtTop = value;
  }

  public TxObjectList SharedWeldPoints
  {
    get => this.m_sharedWeldPoints;
    set => this.m_sharedWeldPoints = value;
  }

  public bool ExecuteAutomaticMatch(ref bool matchFound)
  {
    return CUiROMAutomaticMatchSettings.Criteria != CUiROMDataUtils.AutomaticMatchCriteria.MatchByName ? this.MatchByDistance(ref matchFound) : this.MatchByName(ref matchFound);
  }

  private bool MatchByName(ref bool matchFound)
  {
    bool flag = true;
    if (this.m_targetOperationAtTop.NodeType == CUiROMDataUtils.NodeType.ContinuousOperation)
    {
      flag = this.MatchByNameContOp(ref matchFound);
    }
    else
    {
      for (int index = 0; index < this.TargetChildren.Count; ++index)
      {
        CUiROMTargetInfo targetChild = this.TargetChildren[index];
        if (targetChild.BelongToTarget)
          matchFound = this.FindMatchingLocByName(targetChild, 0, this.SourceChildren.Count - 1);
      }
    }
    return flag;
  }

  private bool MatchByNameContOp(ref bool matchFound)
  {
    bool flag = true;
    if (this.m_targetOperationAtTop.NodeType != CUiROMDataUtils.NodeType.ContinuousOperation)
      return flag;
    StringComparison comparisonType = StringComparison.InvariantCultureIgnoreCase;
    if (CUiROMAutomaticMatchSettings.MatchByNameCaseSensitive)
      comparisonType = StringComparison.InvariantCulture;
    int targetSeamOpIndex = 0;
    for (int index1 = 0; index1 < this.TargetChildren.Count; ++index1)
    {
      CUiROMTargetInfo targetChild = this.TargetChildren[index1];
      if (targetChild.BelongToTarget && targetChild.NodeType != CUiROMDataUtils.NodeType.SeamLocation)
      {
        if (targetChild.NodeType == CUiROMDataUtils.NodeType.SeamOperation)
        {
          int sourceSeamOpIndex = 0;
          ++targetSeamOpIndex;
          if (this.FindMatchingSeamOp(targetSeamOpIndex, ref sourceSeamOpIndex))
          {
            this.MatchSeamLocsOfMatchedSeamOp(index1, sourceSeamOpIndex);
            CUiROMSourceInfo sourceChild = this.SourceChildren[sourceSeamOpIndex];
            targetChild.MatchedObject = sourceChild.NodeObject;
            sourceChild.Matched = true;
            matchFound = true;
          }
        }
        else
        {
          for (int index2 = 0; index2 < this.SourceChildren.Count; ++index2)
          {
            CUiROMSourceInfo sourceChild = this.SourceChildren[index2];
            if (this.AreObjectsCompatible(targetChild.NodeObject, sourceChild.NodeObject) && targetChild.NodeObject.Name.Equals(sourceChild.NodeObject.Name, comparisonType))
            {
              targetChild.MatchedObject = sourceChild.NodeObject;
              sourceChild.Matched = true;
              matchFound = true;
            }
          }
        }
      }
    }
    return flag;
  }

  private bool MatchByDistance(ref bool matchFound)
  {
    bool flag = true;
    if (this.m_targetOperationAtTop.NodeType == CUiROMDataUtils.NodeType.ContinuousOperation)
      return this.MatchByDistanceContOp(ref matchFound);
    ArrayList arrayList1 = new ArrayList();
    ArrayList arrayList2 = new ArrayList();
    for (int index = 0; index < this.TargetChildren.Count; ++index)
    {
      CUiROMTargetInfo targetChild = this.TargetChildren[index];
      if (targetChild.BelongToTarget)
        matchFound = this.FindMatchingLocByDistance(targetChild, 0, this.SourceChildren.Count - 1);
    }
    return flag;
  }

  private bool MatchByDistanceContOp(ref bool matchFound)
  {
    bool flag1 = true;
    if (this.m_targetOperationAtTop.NodeType != CUiROMDataUtils.NodeType.ContinuousOperation)
      return flag1;
    ArrayList arrayList1 = new ArrayList();
    ArrayList arrayList2 = new ArrayList();
    int targetSeamOpIndex = 0;
    for (int index1 = 0; index1 < this.TargetChildren.Count; ++index1)
    {
      CUiROMTargetInfo targetChild = this.TargetChildren[index1];
      if (targetChild.BelongToTarget && targetChild.NodeType != CUiROMDataUtils.NodeType.SeamLocation)
      {
        if (targetChild.NodeType == CUiROMDataUtils.NodeType.SeamOperation)
        {
          int sourceSeamOpIndex = 0;
          ++targetSeamOpIndex;
          if (this.FindMatchingSeamOp(targetSeamOpIndex, ref sourceSeamOpIndex))
          {
            this.MatchSeamLocsOfMatchedSeamOp(index1, sourceSeamOpIndex);
            CUiROMSourceInfo sourceChild = this.SourceChildren[sourceSeamOpIndex];
            targetChild.MatchedObject = sourceChild.NodeObject;
            sourceChild.Matched = true;
          }
        }
        else
        {
          for (int index2 = 0; index2 < this.SourceChildren.Count; ++index2)
          {
            CUiROMSourceInfo sourceChild = this.SourceChildren[index2];
            if (this.AreObjectsCompatible(targetChild.NodeObject, sourceChild.NodeObject))
            {
              CApROMDataManager.LocationPair locationPair = new CApROMDataManager.LocationPair();
              locationPair.m_targetLoc = targetChild;
              locationPair.m_sourceLoc = sourceChild;
              double distanceBetween2Locs = this.CalculateDistanceBetween2Locs(targetChild.NodeObject as ITxLocatableObject, sourceChild.NodeObject as ITxLocatableObject);
              if (distanceBetween2Locs <= CUiROMAutomaticMatchSettings.MaximumDistance || distanceBetween2Locs < 0.25)
              {
                bool flag2 = true;
                if (CUiROMAutomaticMatchSettings.Criteria == CUiROMDataUtils.AutomaticMatchCriteria.MatchByDistanceWithMaxDeviation)
                {
                  flag2 = false;
                  if (this.CalculateDeviationBetween2Locs(targetChild.NodeObject as ITxLocatableObject, sourceChild.NodeObject as ITxLocatableObject) <= CUiROMAutomaticMatchSettings.MaximumDeviation)
                    flag2 = true;
                }
                if (flag2)
                {
                  arrayList1.Add((object) locationPair);
                  arrayList2.Add((object) distanceBetween2Locs);
                }
              }
            }
          }
        }
      }
    }
    Array array1 = (Array) arrayList2.ToArray();
    Array array2 = (Array) arrayList1.ToArray();
    Array.Sort(array1, array2);
    ArrayList arrayList3 = new ArrayList((ICollection) array2);
    for (int index = 0; index < arrayList3.Count; ++index)
    {
      if (arrayList3[index] is CApROMDataManager.LocationPair locationPair)
      {
        CUiROMTargetInfo targetLoc = locationPair.m_targetLoc;
        CUiROMSourceInfo sourceLoc = locationPair.m_sourceLoc;
        if (targetLoc.MatchedObject == null && !sourceLoc.Matched)
        {
          targetLoc.MatchedObject = sourceLoc.NodeObject;
          sourceLoc.Matched = true;
          matchFound = true;
        }
      }
    }
    return flag1;
  }

  private void ClearMatches()
  {
    for (int index = 0; index < this.TargetChildren.Count; ++index)
      this.TargetChildren[index].MatchedObject = (ITxObject) null;
    for (int index = 0; index < this.SourceChildren.Count; ++index)
      this.SourceChildren[index].Matched = false;
  }

  public double CalculateDistanceBetween2Locs(ITxLocatableObject loc1, ITxLocatableObject loc2)
  {
    TxTransformation txTransformation1 = loc1.AbsoluteLocation;
    TxTransformation locationTeachPosition1 = this.GetLocationTeachPosition((ITxObject) loc1);
    if (TxTransformation.op_Inequality(locationTeachPosition1, (TxTransformation) null))
      txTransformation1 = locationTeachPosition1;
    TxTransformation txTransformation2 = loc2.AbsoluteLocation;
    TxTransformation locationTeachPosition2 = this.GetLocationTeachPosition((ITxObject) loc2);
    if (TxTransformation.op_Inequality(locationTeachPosition2, (TxTransformation) null))
      txTransformation2 = locationTeachPosition2;
    TxVector translation1 = txTransformation1.Translation;
    TxVector translation2 = txTransformation2.Translation;
    return Math.Sqrt(Math.Pow(translation2.X - translation1.X, 2.0) + Math.Pow(translation2.Y - translation1.Y, 2.0) + Math.Pow(translation2.Z - translation1.Z, 2.0));
  }

  public double CalculateDeviationBetween2Locs(ITxLocatableObject loc1, ITxLocatableObject loc2)
  {
    TxTransformation txTransformation1 = loc1.AbsoluteLocation;
    TxTransformation locationTeachPosition1 = this.GetLocationTeachPosition((ITxObject) loc1);
    if (TxTransformation.op_Inequality(locationTeachPosition1, (TxTransformation) null))
      txTransformation1 = locationTeachPosition1;
    TxTransformation txTransformation2 = loc2.AbsoluteLocation;
    TxTransformation locationTeachPosition2 = this.GetLocationTeachPosition((ITxObject) loc2);
    if (TxTransformation.op_Inequality(locationTeachPosition2, (TxTransformation) null))
      txTransformation2 = locationTeachPosition2;
    TxVector rotationRpyXyz1 = txTransformation1.RotationRPY_XYZ;
    TxVector rotationRpyXyz2 = txTransformation2.RotationRPY_XYZ;
    double x1 = 0.0;
    double x2 = 0.0;
    TxVector rotationRpyXyz3 = TxTransformationEx.Delta(txTransformation1, txTransformation2).RotationRPY_XYZ;
    switch (loc1)
    {
      case TxWeldLocationOperation _:
        TxWeldOptions.TxPerpendicularAxis perpendicular = TxApplication.Options.Weld.Perpendicular;
        if (perpendicular == null)
        {
          x1 = rotationRpyXyz3.Y;
          x2 = rotationRpyXyz3.Z;
          break;
        }
        if (perpendicular == 1)
        {
          x1 = rotationRpyXyz3.X;
          x2 = rotationRpyXyz3.Z;
          break;
        }
        x1 = rotationRpyXyz3.X;
        x2 = rotationRpyXyz3.Y;
        break;
      case TxRoboticSeamLocationOperation _:
        switch ((int) TxApplication.Options.Continuous.Normal)
        {
          case 0:
          case 3:
            x1 = rotationRpyXyz3.Y;
            x2 = rotationRpyXyz3.Z;
            break;
          case 1:
          case 4:
            x1 = rotationRpyXyz3.X;
            x2 = rotationRpyXyz3.Z;
            break;
          case 2:
          case 5:
            x1 = rotationRpyXyz3.X;
            x2 = rotationRpyXyz3.Y;
            break;
        }
        break;
    }
    return Math.Sqrt(Math.Pow(x1, 2.0) + Math.Pow(x2, 2.0));
  }

  private TxTransformation GetLocationTeachPosition(ITxObject loc)
  {
    TxTransformation locationTeachPosition = (TxTransformation) null;
    if (loc is ITxRoboticLocationOperation)
      locationTeachPosition = this.m_olpUtils.CalcTaughtFrame(loc as ITxRoboticLocationOperation);
    return locationTeachPosition;
  }

  public CUiROMDataUtils.NodeType GetNodeType(ITxObject obj)
  {
    CUiROMDataUtils.NodeType nodeType = CUiROMDataUtils.NodeType.NotSet;
    switch (obj)
    {
      case TxRoboticViaLocationOperation _:
        nodeType = CUiROMDataUtils.NodeType.ViaLocation;
        break;
      case TxGenericRoboticLocationOperation _:
        switch ((obj as TxGenericRoboticLocationOperation).ProcessType)
        {
          case null:
            break;
          case "PICK_OPERATION_TYPE":
            nodeType = CUiROMDataUtils.NodeType.PickLocation;
            break;
          case "PLACE_OPERATION_TYPE":
            nodeType = CUiROMDataUtils.NodeType.PlaceLocation;
            break;
          default:
            nodeType = CUiROMDataUtils.NodeType.GenericLocation;
            break;
        }
        break;
      case TxWeldLocationOperation _:
        TxWeldLocationOperation locationOperation = obj as TxWeldLocationOperation;
        nodeType = !TxWeldLocationOperation.op_Inequality(locationOperation, (ITxObject) null) || !locationOperation.IsProjected ? CUiROMDataUtils.NodeType.UnprojectedWeldLocation : CUiROMDataUtils.NodeType.WeldLocation;
        break;
      case TxObjectFlowLocationOperation _:
        nodeType = CUiROMDataUtils.NodeType.ObjectFlowLocation;
        break;
      case TxRoboticSeamLocationOperation _:
        nodeType = CUiROMDataUtils.NodeType.SeamLocation;
        break;
      case ITxLocatableObject _:
        nodeType = CUiROMDataUtils.NodeType.GenericLocation;
        break;
      case ITxRoboticOperation _ when (obj as ITxRoboticOperation).IsMountedWorkpieceOperation:
        nodeType = CUiROMDataUtils.NodeType.MountedWorkpieceOperation;
        break;
      case ITxWeldOperation _:
        nodeType = CUiROMDataUtils.NodeType.WeldOperation;
        break;
      case TxGenericRoboticOperation _:
        nodeType = CUiROMDataUtils.NodeType.GenericRoboticOperation;
        break;
      case TxObjectFlowOperation _:
        nodeType = CUiROMDataUtils.NodeType.ObjectFlowOperation;
        break;
      case ITxContinuousOperation _:
        nodeType = CUiROMDataUtils.NodeType.ContinuousOperation;
        break;
      case TxRoboticSeamOperation _:
        nodeType = CUiROMDataUtils.NodeType.SeamOperation;
        break;
    }
    return nodeType;
  }

  public bool AreObjectsCompatible(ITxObject firstObj, ITxObject secondObj)
  {
    bool flag = false;
    if (firstObj != null && secondObj != null)
    {
      Type type1 = firstObj.GetType();
      Type type2 = secondObj.GetType();
      if (type1 != (Type) null && type2 != (Type) null && type1.Equals(type2))
        flag = true;
    }
    return flag;
  }

  public void RecreateTargetOperationUiData(ITxObject targetOperation, ITxObject sourceOperation)
  {
    this.m_targetChildren.Clear();
    this.m_targetOperationAtTop = this.CreateTargetOperation(targetOperation, sourceOperation);
    if (!(targetOperation is ITxObjectCollection objectCollection1))
      return;
    TxTypeFilter txTypeFilter1 = new TxTypeFilter();
    txTypeFilter1.AddIncludedType(typeof (ITxLocatableObject));
    txTypeFilter1.AddIncludedType(typeof (TxRoboticSeamOperation));
    TxObjectList directDescendants1 = objectCollection1.GetDirectDescendants((ITxTypeFilter) txTypeFilter1);
    if (directDescendants1 == null || ((Collection<ITxObject>) directDescendants1).Count <= 0)
      return;
    foreach (ITxObject itxObject in (Collection<ITxObject>) directDescendants1)
    {
      if (itxObject is ITxLocatableObject)
      {
        CUiROMTargetInfo targetLocation = this.CreateTargetLocation(itxObject);
        if (targetLocation != null)
          this.m_targetChildren.Add(targetLocation);
      }
      else
      {
        CUiROMTargetInfo targetSeamOperation = this.CreateTargetSeamOperation(itxObject);
        if (targetSeamOperation != null)
        {
          this.m_targetChildren.Add(targetSeamOperation);
          if (itxObject is ITxObjectCollection objectCollection2)
          {
            TxTypeFilter txTypeFilter2 = new TxTypeFilter();
            txTypeFilter2.AddIncludedType(typeof (TxRoboticSeamLocationOperation));
            TxObjectList directDescendants2 = objectCollection2.GetDirectDescendants((ITxTypeFilter) txTypeFilter2);
            if (directDescendants2 != null && ((Collection<ITxObject>) directDescendants2).Count > 0)
            {
              foreach (ITxObject location in (Collection<ITxObject>) directDescendants2)
              {
                CUiROMTargetInfo targetSeamLocation = this.CreateTargetSeamLocation(location);
                if (targetSeamLocation != null)
                  this.m_targetChildren.Add(targetSeamLocation);
              }
            }
          }
        }
      }
    }
  }

  public ArrayList InsertSourceLocationsToTargetOperation(
    int index,
    List<CUiROMSourceInfo> sourceLocationsList)
  {
    ArrayList targetOperation = (ArrayList) null;
    if (sourceLocationsList != null && sourceLocationsList.Count > 0)
    {
      List<CUiROMTargetInfo> cuiRomTargetInfoList = new List<CUiROMTargetInfo>();
      foreach (CUiROMSourceInfo sourceLocations in sourceLocationsList)
      {
        CUiROMTargetInfo grayedTargetLocation = this.CreateGrayedTargetLocation(sourceLocations);
        if (grayedTargetLocation != null)
        {
          if (this.m_targetOperationAtTop.NodeType == CUiROMDataUtils.NodeType.ContinuousOperation && grayedTargetLocation.NodeType == CUiROMDataUtils.NodeType.SeamLocation)
            ++grayedTargetLocation.Level;
          cuiRomTargetInfoList.Add(grayedTargetLocation);
        }
      }
      if (cuiRomTargetInfoList.Count > 0)
      {
        if (index < 0)
          index = 0;
        if (index >= this.m_targetChildren.Count)
          this.m_targetChildren.AddRange((IEnumerable<CUiROMTargetInfo>) cuiRomTargetInfoList);
        else
          this.m_targetChildren.InsertRange(index, (IEnumerable<CUiROMTargetInfo>) cuiRomTargetInfoList);
        this.RecreateSourceLocationsCanBeAddedStatus();
        targetOperation = new ArrayList((ICollection) cuiRomTargetInfoList);
      }
    }
    return targetOperation;
  }

  public void RemoveTargetChildren(List<CUiROMTargetInfo> childList)
  {
    if (childList == null || childList.Count <= 0)
      return;
    foreach (CUiROMTargetInfo child in childList)
    {
      if (this.m_targetChildren.Contains(child))
      {
        this.UpdateSourceLocationCanBeAddedStatus(child.NodeObject, true);
        this.m_targetChildren.Remove(child);
      }
    }
  }

  public void RemoveSourceChildren(List<CUiROMSourceInfo> locationsList)
  {
    if (locationsList == null || locationsList.Count <= 0)
      return;
    foreach (CUiROMSourceInfo locations in locationsList)
    {
      if (this.m_sourceChildren.Contains(locations))
        this.m_sourceChildren.Remove(locations);
    }
  }

  public CUiROMRoboticTargetOperationInfo CreateTargetOperation(
    ITxObject targetOperation,
    ITxObject sourceOperation)
  {
    CUiROMRoboticTargetOperationInfo targetOperation1 = new CUiROMRoboticTargetOperationInfo();
    targetOperation1.NodeObject = targetOperation;
    if (targetOperation != null)
    {
      targetOperation1.NiceName = targetOperation.Name;
      targetOperation1.MatchedOperation = sourceOperation;
      targetOperation1.MergeScheme = CApROMStringTable.Operation;
      targetOperation1.ShouldMerge = false;
      targetOperation1.NodeType = this.GetNodeType(targetOperation);
      targetOperation1.Level = 0;
      targetOperation1.State = CUiROMDataUtils.CellState.Normal;
      targetOperation1.SetCustomStyle(CUiROMDataUtils.CellStyleCategory.Normal, "targetOperationNormalStyle");
      targetOperation1.SetCustomStyle(CUiROMDataUtils.CellStyleCategory.Select, "sourceOperationSelectStyle");
    }
    return targetOperation1;
  }

  public CUiROMTargetInfo CreateTargetLocation(ITxObject location)
  {
    CUiROMTargetInfo targetLocation = new CUiROMTargetInfo();
    targetLocation.NodeObject = location;
    if (location != null)
    {
      targetLocation.NiceName = location.Name;
      targetLocation.NodeType = this.GetNodeType(location);
      targetLocation.Level = 1;
      targetLocation.BelongToTarget = true;
      targetLocation.Distance = 0.0;
      targetLocation.MatchedObject = (ITxObject) null;
      targetLocation.ShouldMerge = false;
      targetLocation.State = CUiROMDataUtils.CellState.Normal;
      targetLocation.SetCustomStyle(CUiROMDataUtils.CellStyleCategory.Normal, "targetLocationNormalStyle");
      targetLocation.SetCustomStyle(CUiROMDataUtils.CellStyleCategory.Select, "targetLocationSelectStyle");
    }
    return targetLocation;
  }

  private CUiROMTargetInfo CreateGrayedTargetLocation(CUiROMSourceInfo sourceInfo)
  {
    CUiROMTargetInfo grayedTargetLocation = new CUiROMTargetInfo();
    if (sourceInfo != null)
    {
      grayedTargetLocation.NodeObject = sourceInfo.NodeObject;
      grayedTargetLocation.NiceName = sourceInfo.NiceName;
      grayedTargetLocation.NodeType = sourceInfo.NodeType;
      grayedTargetLocation.Level = 1;
      grayedTargetLocation.BelongToTarget = false;
      grayedTargetLocation.Distance = 0.0;
      grayedTargetLocation.MatchedObject = sourceInfo.NodeObject;
      grayedTargetLocation.ShouldMerge = true;
      grayedTargetLocation.State = CUiROMDataUtils.CellState.Normal;
      grayedTargetLocation.SetCustomStyle(CUiROMDataUtils.CellStyleCategory.Normal, "targetGrayedLocationNormalStyle");
      grayedTargetLocation.SetCustomStyle(CUiROMDataUtils.CellStyleCategory.Select, "targetGrayedLocationSelectStyle");
    }
    return grayedTargetLocation;
  }

  public void RecreateSourceOperationUiData(ITxObject sourceOperation)
  {
    this.m_sourceChildren.Clear();
    this.m_sourceOperationAtTop = this.CreateSourceOperation(sourceOperation);
    if (sourceOperation is ITxObjectCollection objectCollection1)
    {
      TxTypeFilter txTypeFilter1 = new TxTypeFilter();
      txTypeFilter1.AddIncludedType(typeof (ITxLocatableObject));
      txTypeFilter1.AddIncludedType(typeof (TxRoboticSeamOperation));
      TxObjectList directDescendants1 = objectCollection1.GetDirectDescendants((ITxTypeFilter) txTypeFilter1);
      if (directDescendants1 != null && ((Collection<ITxObject>) directDescendants1).Count > 0)
      {
        foreach (ITxObject itxObject in (Collection<ITxObject>) directDescendants1)
        {
          if (itxObject is ITxLocatableObject)
          {
            CUiROMSourceInfo sourceLocation = this.CreateSourceLocation(itxObject);
            if (sourceLocation != null)
              this.m_sourceChildren.Add(sourceLocation);
          }
          else
          {
            CUiROMSourceInfo sourceSeamOperation = this.CreateSourceSeamOperation(itxObject);
            if (sourceSeamOperation != null)
            {
              this.m_sourceChildren.Add(sourceSeamOperation);
              if (itxObject is ITxObjectCollection objectCollection)
              {
                TxTypeFilter txTypeFilter2 = new TxTypeFilter();
                txTypeFilter2.AddIncludedType(typeof (TxRoboticSeamLocationOperation));
                TxObjectList directDescendants2 = objectCollection.GetDirectDescendants((ITxTypeFilter) txTypeFilter2);
                if (directDescendants2 != null && ((Collection<ITxObject>) directDescendants2).Count > 0)
                {
                  foreach (ITxObject location in (Collection<ITxObject>) directDescendants2)
                  {
                    CUiROMSourceInfo sourceSeamLocation = this.CreateSourceSeamLocation(location);
                    if (sourceSeamLocation != null)
                      this.m_sourceChildren.Add(sourceSeamLocation);
                  }
                }
              }
            }
          }
        }
      }
    }
    this.RecreateSourceLocationsCanBeAddedStatus();
  }

  public CUiROMRoboticSourceOperationInfo CreateSourceOperation(ITxObject sourceOperation)
  {
    CUiROMRoboticSourceOperationInfo sourceOperation1 = new CUiROMRoboticSourceOperationInfo();
    sourceOperation1.NodeObject = sourceOperation;
    if (sourceOperation != null)
    {
      sourceOperation1.NiceName = sourceOperation.Name;
      sourceOperation1.NodeType = this.GetNodeType(sourceOperation);
      sourceOperation1.Level = 0;
      sourceOperation1.State = CUiROMDataUtils.CellState.Normal;
      sourceOperation1.SetCustomStyle(CUiROMDataUtils.CellStyleCategory.Normal, "sourceOperationNormalStyle");
      sourceOperation1.SetCustomStyle(CUiROMDataUtils.CellStyleCategory.Select, "sourceOperationSelectStyle");
    }
    return sourceOperation1;
  }

  public CUiROMSourceInfo CreateSourceLocation(ITxObject location)
  {
    CUiROMSourceInfo sourceLocation = new CUiROMSourceInfo();
    sourceLocation.NodeObject = location;
    if (location != null)
    {
      sourceLocation.NiceName = location.Name;
      sourceLocation.NodeType = this.GetNodeType(location);
      sourceLocation.Level = 1;
      sourceLocation.Matched = false;
      sourceLocation.State = CUiROMDataUtils.CellState.Normal;
      sourceLocation.SetCustomStyle(CUiROMDataUtils.CellStyleCategory.Normal, "sourceLocationNormalStyle");
      sourceLocation.SetCustomStyle(CUiROMDataUtils.CellStyleCategory.Select, "sourceLocationSelectStyle");
    }
    return sourceLocation;
  }

  public CUiROMTargetInfo CreateTargetSeamOperation(ITxObject operation)
  {
    CUiROMTargetInfo targetSeamOperation = new CUiROMTargetInfo();
    targetSeamOperation.NodeObject = operation;
    if (operation != null)
    {
      targetSeamOperation.NiceName = operation.Name;
      targetSeamOperation.NodeType = this.GetNodeType(operation);
      targetSeamOperation.Level = 1;
      targetSeamOperation.MatchedObject = (ITxObject) null;
      targetSeamOperation.ShouldMerge = false;
      targetSeamOperation.State = CUiROMDataUtils.CellState.Normal;
      targetSeamOperation.SetCustomStyle(CUiROMDataUtils.CellStyleCategory.Normal, "targetLocationNormalStyle");
      targetSeamOperation.SetCustomStyle(CUiROMDataUtils.CellStyleCategory.Select, "targetLocationSelectStyle");
    }
    return targetSeamOperation;
  }

  public CUiROMTargetInfo CreateTargetSeamLocation(ITxObject location)
  {
    CUiROMTargetInfo targetSeamLocation = new CUiROMTargetInfo();
    targetSeamLocation.NodeObject = location;
    if (location != null)
    {
      targetSeamLocation.NiceName = location.Name;
      targetSeamLocation.NodeType = this.GetNodeType(location);
      targetSeamLocation.Level = 2;
      targetSeamLocation.BelongToTarget = true;
      targetSeamLocation.Distance = 0.0;
      targetSeamLocation.MatchedObject = (ITxObject) null;
      targetSeamLocation.ShouldMerge = false;
      targetSeamLocation.State = CUiROMDataUtils.CellState.Normal;
      targetSeamLocation.SetCustomStyle(CUiROMDataUtils.CellStyleCategory.Normal, "targetLocationNormalStyle");
      targetSeamLocation.SetCustomStyle(CUiROMDataUtils.CellStyleCategory.Select, "targetLocationSelectStyle");
    }
    return targetSeamLocation;
  }

  public CUiROMTargetInfo CreateGrayedTargetSeamLocation(CUiROMSourceInfo sourceInfo)
  {
    CUiROMTargetInfo targetSeamLocation = new CUiROMTargetInfo();
    if (sourceInfo != null)
    {
      targetSeamLocation.NodeObject = sourceInfo.NodeObject;
      targetSeamLocation.NiceName = sourceInfo.NiceName;
      targetSeamLocation.NodeType = sourceInfo.NodeType;
      targetSeamLocation.Level = 2;
      targetSeamLocation.BelongToTarget = false;
      targetSeamLocation.Distance = 0.0;
      targetSeamLocation.MatchedObject = sourceInfo.NodeObject;
      targetSeamLocation.ShouldMerge = true;
      targetSeamLocation.State = CUiROMDataUtils.CellState.Normal;
      targetSeamLocation.SetCustomStyle(CUiROMDataUtils.CellStyleCategory.Normal, "targetGrayedLocationNormalStyle");
      targetSeamLocation.SetCustomStyle(CUiROMDataUtils.CellStyleCategory.Select, "targetGrayedLocationSelectStyle");
    }
    return targetSeamLocation;
  }

  public CUiROMSourceInfo CreateSourceSeamOperation(ITxObject seamOperation)
  {
    CUiROMSourceInfo sourceSeamOperation = new CUiROMSourceInfo();
    sourceSeamOperation.NodeObject = seamOperation;
    if (seamOperation != null)
    {
      sourceSeamOperation.NiceName = seamOperation.Name;
      sourceSeamOperation.NodeType = this.GetNodeType(seamOperation);
      sourceSeamOperation.Level = 1;
      sourceSeamOperation.Matched = false;
      sourceSeamOperation.State = CUiROMDataUtils.CellState.Normal;
      sourceSeamOperation.SetCustomStyle(CUiROMDataUtils.CellStyleCategory.Normal, "sourceLocationNormalStyle");
      sourceSeamOperation.SetCustomStyle(CUiROMDataUtils.CellStyleCategory.Select, "sourceLocationSelectStyle");
    }
    return sourceSeamOperation;
  }

  public CUiROMSourceInfo CreateSourceSeamLocation(ITxObject location)
  {
    CUiROMSourceInfo sourceSeamLocation = new CUiROMSourceInfo();
    sourceSeamLocation.NodeObject = location;
    if (location != null)
    {
      sourceSeamLocation.NiceName = location.Name;
      sourceSeamLocation.NodeType = this.GetNodeType(location);
      sourceSeamLocation.Level = 2;
      sourceSeamLocation.Matched = false;
      sourceSeamLocation.State = CUiROMDataUtils.CellState.Normal;
      sourceSeamLocation.SetCustomStyle(CUiROMDataUtils.CellStyleCategory.Normal, "sourceLocationNormalStyle");
      sourceSeamLocation.SetCustomStyle(CUiROMDataUtils.CellStyleCategory.Select, "sourceLocationSelectStyle");
    }
    return sourceSeamLocation;
  }

  public void RefreshMatchedIndication(ITxObject matchedObject)
  {
    if (matchedObject == null)
      return;
    int num = 0;
    foreach (CUiROMTargetInfo targetChild in this.m_targetChildren)
    {
      if (targetChild != null && targetChild.MatchedObject == matchedObject)
        ++num;
    }
    foreach (CUiROMSourceInfo sourceChild in this.m_sourceChildren)
    {
      if (sourceChild != null && sourceChild.NodeObject == matchedObject)
      {
        if (num < 1 && sourceChild.Matched)
        {
          sourceChild.Matched = false;
          break;
        }
        if (num <= 0 || sourceChild.Matched)
          break;
        sourceChild.Matched = true;
        break;
      }
    }
  }

  private void AddSharedWeldPoint(TxWeldLocationOperation weldLocation)
  {
    if (!TxWeldLocationOperation.op_Inequality(weldLocation, (ITxObject) null))
      return;
    TxWeldPoint weldPoint = weldLocation.WeldPoint;
    if (!TxWeldPoint.op_Inequality(weldPoint, (ITxObject) null) || ((Collection<ITxObject>) this.m_sharedWeldPoints).Contains((ITxObject) weldPoint))
      return;
    ((Collection<ITxObject>) this.m_sharedWeldPoints).Add((ITxObject) weldPoint);
  }

  private void AddSharedSeamOp(TxWeldLocationOperation seamOp)
  {
    if (!TxWeldLocationOperation.op_Inequality(seamOp, (ITxObject) null))
      return;
    TxWeldPoint weldPoint = seamOp.WeldPoint;
    if (!TxWeldPoint.op_Inequality(weldPoint, (ITxObject) null) || ((Collection<ITxObject>) this.m_sharedWeldPoints).Contains((ITxObject) weldPoint))
      return;
    ((Collection<ITxObject>) this.m_sharedWeldPoints).Add((ITxObject) weldPoint);
  }

  private void RemoveSharedWeldPoint(TxWeldLocationOperation weldLocation)
  {
    if (!TxWeldLocationOperation.op_Inequality(weldLocation, (ITxObject) null))
      return;
    TxWeldPoint weldPoint = weldLocation.WeldPoint;
    if (!TxWeldPoint.op_Inequality(weldPoint, (ITxObject) null) || !((Collection<ITxObject>) this.m_sharedWeldPoints).Contains((ITxObject) weldPoint))
      return;
    ((Collection<ITxObject>) this.m_sharedWeldPoints).Remove((ITxObject) weldPoint);
  }

  private void ClearSharedWeldPoints() => ((Collection<ITxObject>) this.m_sharedWeldPoints).Clear();

  private bool HasSharedWeldPoint(TxWeldLocationOperation weldLocation)
  {
    bool flag = false;
    TxWeldPoint weldPoint = weldLocation.WeldPoint;
    if (TxWeldPoint.op_Inequality(weldPoint, (ITxObject) null) && ((Collection<ITxObject>) this.m_sharedWeldPoints).Contains((ITxObject) weldPoint))
      flag = true;
    return flag;
  }

  private TxWeldPoint GetAssignedWeldPoint(TxWeldLocationOperation weldLocation)
  {
    TxWeldPoint assignedWeldPoint = (TxWeldPoint) null;
    if (TxWeldLocationOperation.op_Inequality(weldLocation, (ITxObject) null))
    {
      try
      {
        assignedWeldPoint = weldLocation.WeldPoint;
      }
      catch (Exception ex)
      {
        assignedWeldPoint = (TxWeldPoint) null;
      }
    }
    return assignedWeldPoint;
  }

  public void RecreateSourceLocationsCanBeAddedStatus()
  {
    this.ClearSharedWeldPoints();
    for (int index = 0; index < this.m_targetChildren.Count; ++index)
    {
      CUiROMTargetInfo targetChild = this.m_targetChildren[index];
      try
      {
        if (targetChild != null)
        {
          if (targetChild.NodeObject != null)
          {
            TxWeldLocationOperation nodeObject = targetChild.NodeObject as TxWeldLocationOperation;
            if (TxWeldLocationOperation.op_Inequality(nodeObject, (ITxObject) null))
              this.AddSharedWeldPoint(nodeObject);
          }
        }
      }
      catch (Exception ex)
      {
      }
    }
    for (int index = 0; index < this.m_sourceChildren.Count; ++index)
    {
      CUiROMSourceInfo sourceChild = this.m_sourceChildren[index];
      try
      {
        if (sourceChild != null)
        {
          if (sourceChild.NodeObject != null)
          {
            TxWeldLocationOperation nodeObject = sourceChild.NodeObject as TxWeldLocationOperation;
            if (TxWeldLocationOperation.op_Inequality(nodeObject, (ITxObject) null) && this.HasSharedWeldPoint(nodeObject))
              sourceChild.CanBeAdded = false;
            else if (!sourceChild.CanBeAdded)
              sourceChild.CanBeAdded = true;
          }
        }
      }
      catch (Exception ex)
      {
      }
    }
  }

  public void UpdateSourceLocationCanBeAddedStatus(ITxObject weldLocation, bool isRemoved)
  {
    TxWeldPoint assignedWeldPoint = this.GetAssignedWeldPoint(weldLocation as TxWeldLocationOperation);
    if (!TxWeldPoint.op_Inequality(assignedWeldPoint, (ITxObject) null) || !((Collection<ITxObject>) this.m_sharedWeldPoints).Contains((ITxObject) assignedWeldPoint))
      return;
    for (int index = 0; index < this.m_sourceChildren.Count; ++index)
    {
      CUiROMSourceInfo sourceChild = this.m_sourceChildren[index];
      if (sourceChild != null && sourceChild.NodeObject != null && TxWeldPoint.op_Equality(this.GetAssignedWeldPoint(sourceChild.NodeObject as TxWeldLocationOperation), (ITxObject) assignedWeldPoint))
        sourceChild.CanBeAdded = isRemoved;
    }
  }

  private bool FindMatchingSeamOp(int targetSeamOpIndex, ref int sourceSeamOpIndex)
  {
    bool matchingSeamOp = false;
    int num = 0;
    for (int index = 0; !matchingSeamOp && index < this.SourceChildren.Count; ++index)
    {
      if (this.SourceChildren[index].NodeType == CUiROMDataUtils.NodeType.SeamOperation)
      {
        ++num;
        if (num == targetSeamOpIndex)
        {
          matchingSeamOp = true;
          sourceSeamOpIndex = index;
        }
      }
    }
    return matchingSeamOp;
  }

  private int MatchSeamLocsOfMatchedSeamOp(int targetSeamOpIndex, int sourceSeamOpIndex)
  {
    int num1 = 0;
    bool flag = false;
    if (targetSeamOpIndex + 1 >= this.TargetChildren.Count || sourceSeamOpIndex + 1 >= this.SourceChildren.Count)
      flag = true;
    for (int index = targetSeamOpIndex + 1; !flag && index < this.TargetChildren.Count; ++index)
    {
      CUiROMTargetInfo targetChild = this.TargetChildren[index];
      if (targetChild.BelongToTarget)
      {
        if (targetChild.NodeType == CUiROMDataUtils.NodeType.SeamLocation)
        {
          int num2 = this.CountSeamLocsOfSeamOp(sourceSeamOpIndex);
          if (num2 > 0 && this.FindMatchingLoc(targetChild, sourceSeamOpIndex + 1, sourceSeamOpIndex + num2))
            ++num1;
        }
        else
          flag = true;
      }
    }
    return num1;
  }

  private bool FindMatchingLoc(CUiROMTargetInfo targetChild, int i_from, int i_to)
  {
    return CUiROMAutomaticMatchSettings.Criteria != CUiROMDataUtils.AutomaticMatchCriteria.MatchByName ? this.FindMatchingLocByDistance(targetChild, i_from, i_to) : this.FindMatchingLocByName(targetChild, i_from, i_to);
  }

  private bool FindMatchingLocByDistance(CUiROMTargetInfo targetChild, int i_from, int i_to)
  {
    bool matchingLocByDistance = false;
    ArrayList arrayList1 = new ArrayList();
    ArrayList arrayList2 = new ArrayList();
    for (int index = i_from; index <= i_to; ++index)
    {
      CUiROMSourceInfo sourceChild = this.SourceChildren[index];
      if (this.AreObjectsCompatible(targetChild.NodeObject, sourceChild.NodeObject))
      {
        CApROMDataManager.LocationPair locationPair = new CApROMDataManager.LocationPair();
        locationPair.m_targetLoc = targetChild;
        locationPair.m_sourceLoc = sourceChild;
        double distanceBetween2Locs = this.CalculateDistanceBetween2Locs(targetChild.NodeObject as ITxLocatableObject, sourceChild.NodeObject as ITxLocatableObject);
        if (distanceBetween2Locs <= CUiROMAutomaticMatchSettings.MaximumDistance + 0.001 || distanceBetween2Locs < 0.25)
        {
          bool flag = true;
          if (CUiROMAutomaticMatchSettings.Criteria == CUiROMDataUtils.AutomaticMatchCriteria.MatchByDistanceWithMaxDeviation)
          {
            flag = false;
            if (this.CalculateDeviationBetween2Locs(targetChild.NodeObject as ITxLocatableObject, sourceChild.NodeObject as ITxLocatableObject) <= CUiROMAutomaticMatchSettings.MaximumDeviation + 0.001)
              flag = true;
          }
          if (flag)
          {
            arrayList1.Add((object) locationPair);
            arrayList2.Add((object) distanceBetween2Locs);
          }
        }
      }
    }
    Array array1 = (Array) arrayList2.ToArray();
    Array array2 = (Array) arrayList1.ToArray();
    Array.Sort(array1, array2);
    ArrayList arrayList3 = new ArrayList((ICollection) array2);
    for (int index = 0; index < arrayList3.Count; ++index)
    {
      if (arrayList3[index] is CApROMDataManager.LocationPair locationPair)
      {
        CUiROMTargetInfo targetLoc = locationPair.m_targetLoc;
        CUiROMSourceInfo sourceLoc = locationPair.m_sourceLoc;
        if (targetLoc.MatchedObject == null && !sourceLoc.Matched)
        {
          targetLoc.MatchedObject = sourceLoc.NodeObject;
          sourceLoc.Matched = true;
          matchingLocByDistance = true;
        }
      }
    }
    return matchingLocByDistance;
  }

  private bool FindMatchingLocByName(CUiROMTargetInfo targetChild, int i_from, int i_to)
  {
    bool matchingLocByName = false;
    StringComparison comparisonType = StringComparison.InvariantCultureIgnoreCase;
    if (CUiROMAutomaticMatchSettings.MatchByNameCaseSensitive)
      comparisonType = StringComparison.InvariantCulture;
    for (int index = i_from; index <= i_to; ++index)
    {
      CUiROMSourceInfo sourceChild = this.SourceChildren[index];
      if (!sourceChild.Matched && targetChild.MatchedObject == null && this.AreObjectsCompatible(targetChild.NodeObject, sourceChild.NodeObject) && targetChild.NodeObject.Name.Equals(sourceChild.NodeObject.Name, comparisonType))
      {
        targetChild.MatchedObject = sourceChild.NodeObject;
        sourceChild.Matched = true;
        matchingLocByName = true;
      }
    }
    return matchingLocByName;
  }

  private int CountSeamLocsOfSeamOp(int i_seamOp)
  {
    int num = 0;
    bool flag = false;
    if (i_seamOp >= this.SourceChildren.Count)
      flag = true;
    for (int index = i_seamOp + 1; !flag && index < this.SourceChildren.Count; ++index)
    {
      if (this.SourceChildren[index].NodeType == CUiROMDataUtils.NodeType.SeamLocation)
        ++num;
      else
        flag = true;
    }
    return num;
  }

  internal class LocationPair
  {
    internal CUiROMTargetInfo m_targetLoc;
    internal CUiROMSourceInfo m_sourceLoc;
  }
}
